using FutureCoreDesign;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(CoordinateLigature))]
public class CoordinateLigature_Inspector : Editor
{
    private List<Transform> coordinates;

    private GUIStyle style = new GUIStyle();

    void OnEnable()
    {
        coordinates = ((CoordinateLigature)target).coordinates;

        style.fontStyle = FontStyle.Normal;
        style.fontSize = 15;
    }

    void OnSceneGUI()
    {
        coordinates = ((CoordinateLigature)serializedObject.targetObject).coordinates;

        Handles.color = Color.red;
        if (coordinates.Count < 2) return;
        for (int i = 0; i < coordinates.Count; i++)
        {
            Transform coordTF = coordinates[i];
            if (coordTF == null)
            {
                return;
            }
            coordTF.position = Handles.PositionHandle(coordTF.position, Quaternion.identity);
            Handles.SphereHandleCap(i, coordTF.position, Quaternion.identity, 0.25f, EventType.Ignore);
            if (i + 1 < coordinates.Count)
            {
                if (coordinates[i + 1] == null)
                {
                    return;
                }
                Handles.DrawLine(coordTF.position, coordinates[i + 1].position);
            }

            string text = null;
            if (i != 0)
            {
                text = string.Format("[{0}] {1}", i, Vector3.Distance(coordinates[i - 1].position, coordinates[i].position).ToString("0.00"));
            }
            else
            {
                text = string.Format("[{0}]", i);
            }
            Handles.Label(coordTF.position, text, style);
        }
    }
}